package com.lxj.controller.sys;

import com.lxj.common.Result;
import com.lxj.domain.ActiveUser;
import com.lxj.domain.SysPermission;
import com.lxj.dto.SysPermissionDto;
import com.lxj.service.PermissionService;
import com.lxj.vo.DTreeVo;
import com.lxj.vo.TreeNode;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author lxj
 * @date 2021/6/18 18:37
 * @Description:权限的控制器
 */
@RestController
@RequestMapping("/sysperms")
public class SysPermissionController {

    @Autowired
    private PermissionService permissionService;

    /**
     * 查询用户的菜单数据
     * @return
     */
    @GetMapping("/tree.do")
    public List<TreeNode> getTree(){
        Subject subject = SecurityUtils.getSubject();
        ActiveUser principal = (ActiveUser) subject.getPrincipal();
        return principal.getMenus();
    }

    /**
     * 分页查询树的数据
     * @param dto
     * @return
     */
    @RequestMapping("/findByPage.do")
    public Result findByPage(SysPermissionDto dto){
        List<SysPermission> permissions = permissionService.findByPage(dto);
        return new Result(permissions);
    }

    /**
     * 新增权限的方法
     * @param dto
     * @return
     */
    @PostMapping("/add.do")
    public Result addPermission(@RequestBody SysPermissionDto dto){
        return permissionService.addPermission(dto);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PostMapping("/delete.do/{ids}")
    public Result batchDelete(@PathVariable("ids") Integer[] ids){
        return permissionService.batchDelete(ids);
    }

    /**
     * 修改用户
     * @param dto
     * @return
     */
    @PostMapping("/update.do")
    public Result updatePermission(@RequestBody SysPermissionDto dto){
        return permissionService.updatePermission(dto);
    }

    /**
     * 查询角色的权限
     * @param roleId
     * @return
     */
    @RequestMapping("/getRolePerms.do")
    public Result getPermsByRoleId(Integer roleId) {
        // 1 得到当前角色的权限
        List<Integer> permsIds = permissionService.getPermsByRoleId(roleId);
        // 2 我们需要得到所有的权限
        List<SysPermission> sysPermissions = permissionService.selectAllSysPermission();
        List<DTreeVo> dTreeVos = convertAndMapping(sysPermissions, permsIds);
        return new Result(dTreeVos);
    }


    /**
     * 给角色分配权限
     * @param roleId   角色的id
     * @param permsIds 权限的集合
     * @return
     */
    @RequestMapping("/assign.do")
    public Result assign(Integer roleId, @RequestBody Integer[] permsIds) {
        permissionService.assign(roleId, permsIds);
        return new Result();
    }

    /**
     * 得到一个DTree 格式的树
     * @param sysPermissions 权限数据
     * @param permsIds       要选择的权限
     * @return
     */
    private List<DTreeVo> convertAndMapping(List<SysPermission> sysPermissions, List<Integer> permsIds) {
        List<DTreeVo> dTreeVos = null;
        if (sysPermissions != null && sysPermissions.size() > 0) {
            dTreeVos = new ArrayList<>();
            for (SysPermission sysPermission : sysPermissions) {
                DTreeVo dTreeVo = new DTreeVo();
                BeanUtils.copyProperties(sysPermission, dTreeVo);
                // 选择和不选择
                Map<String, Object> checked = new HashMap<>();
                if (permsIds.contains(dTreeVo.getId())) {
                    checked.put("type", 0);
                    checked.put("checked", 1);
                } else {
                    checked.put("type", 0);
                    checked.put("checked", 0);
                }
                dTreeVo.setCheckArr(checked);
                dTreeVos.add(dTreeVo);
            }
            return dTreeVos;
        }
        return Collections.emptyList();
    }
}
